#!/bin/bash

### ========= 全局变量 ===========
scriptWorkd=$(cd "$(dirname $0)";pwd)
path_config=${scriptWorkd}/../config-of-many
root_Workd=${scriptWorkd}/..
### ==============================


if [[ -f ${HOME}/bin/basic_func.sh ]];then  ### 存在才载入
    source ${HOME}/bin/basic_func.sh
elif [[ -f ${scriptWorkd}/../bin/basic_func.sh ]];then
    source ${scriptWorkd}/../bin/basic_func.sh
elif [[ -f ./bin/basic_func.sh ]];then
    source ./bin/basic_func.sh
fi

function main () {
set -e

    ## 开始信息
    echo -e "${blue4}$(basename $0)${normal}"

    which_cp="old"
    case ${which_cp} in 
        "new")
            which rsync>/dev/null
            if [[ $? == 0 ]];then
                mycp="rsync -ah --info=progress2"
                arg_dir="--recursive --force "
                arg_file="--force "
            else
                echo "未安装rsync，改为使用默认cp"
                mycp=$(which cp)
                arg_dir="--recursive --force "
                arg_file="--force "
            fi
            ;;
        "old")
            mycp=$(which cp)
                arg_dir="--recursive --force "
                arg_file="--force "
            ;;
        *)
            echo "未指定cp"
            exit 1
            ;;
    esac
    
    TIMEOUT=5
    flag=$((1))
    while [[ $flag == 1 ]];do
        echo -e "${yellow}请选择用途：(Choose usage:)${normal}"
        options=("私人(Private)" "公共(Public)")
        for i in ${!options[@]}; do
            echo -e "$(($i+1))) ${options[i]}"
        done
        printf "输入数字以选择：(Type NUMBER to select:)"
        set +e
        read -t $TIMEOUT version
        if [[ $? == 142 ]];then
            echo ""
            version=${version:-$((2))}
        fi
        if [[ ${version} == 1 ]];then 
            echo -e "已选择: ${red1}${options[$(($version-1))]}${normal}"
            break;
        else if [[ $version == 2 ]];then
            echo -e "已选择: ${green1}${options[$(($version-1))]}${normal}"
            break;
        else
            echo -e ""
        fi
        fi
        set -e
    done
    sleep 2
    
    echo ""
    flag=$((1))
    while [[ $flag == 1 ]];do
        echo -e "${yellow}接下来请选择复制的部分：(Next choose which to copy :)${normal}"
        str=("所有(all)" "仅配置文件(only rc files)" "仅插件(only addons)")
        for i in ${!str[@]}; do
            echo -e "$(($i+1))) ${str[i]}"
        done
        printf "输入数字以选择：(Type NUMBER to select:)"
        set +e
        read -t $TIMEOUT part
        if [[ $? == 142 ]];then
            echo ""
            part=${part:-$((1))}
        fi
        case ${part} in 
            1|2|3)
            if [[ ${part} == 1 || ${part} == 3 ]]; then
                echo -e "复制插件中"
                # ${mycp} -rf         ${path_config}/.local ~/
                ${mycp} -rf         ${path_config}/.tmux ~/
            fi
            ### 复制配置文本
            if [[ ${part} == 1 || ${part} == 2 ]]; then
                echo -e "复制配置文件中"
                ### cp和rsync的 DEST 都应该为目录
                ${mycp} ${arg_dir}  ${path_config}/.config ~/
                # ${mycp} ${arg_dir} ./UltiSnips ~/.local/share/nvim/plugged/vim-snippets/
                ${mycp} ${arg_file} ${path_config}/.tmux.conf ~/
                ${mycp} ${arg_file} ${path_config}/.inputrc ~/
                ${mycp} ${arg_file} ${path_config}/old_bash_completion ~/
                ${mycp} ${arg_file} ${path_config}/.bash* ~/
    
                ${mycp} ${arg_dir}  ${root_Workd}/bin ~/
                insert "if [ -f ~/.bashrc.custom ];then . ~/.bashrc.custom; fi" ~/.bashrc
            fi
            break;
            ;;
            *)
            echo -e ""
        esac
        set -e
    done
    sleep 2
    
    ### 对外复制时做的修改
    if [[ ${version} == 2 ]]; then
        filename=${HOME}/.config/nvim/lua/core/colorscheme.lua
        sed -i "s/local switch=0/local switch=1/" ${filename}
    
        filename=${HOME}/.tmux.conf
        sed -i "s/switch=0 ### tag_locatation/switch=1 ### tag_locatation/" ${filename}
        filename=${HOME}/.bashrc.custom
        sed -i "s#\${psmagenta1}\$(getSystemType)\${psnormal}#\${psred1}\$(getSystemType)\${psnormal}#" ${filename}
        # echo "version=${version}"
    fi
    
    
    ## 结束信息
    echo -e "${green4}\n$(basename $0)\n${normal}"
    set +e
}
main $@

